<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Notice extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 列表
     */
    public function notice_list(){
        $user_id = $this->auth->id;
        $offset = $this->request->post("offset"); // 第几页
        $offset = empty( $offset) ? 0 :  bcsub($offset,1);
        $limit = $this->request->post("limit"); // 一页显示几行
        $limit = empty($limit) ? 10 :  $limit;
        $page = empty($offset) ? 0 :  bcmul($offset,$limit);
        $lists = Db::name("notice")
            ->order('id', 'DESC')
            ->limit($page,$limit)->select();
        $config = config('upload');
        foreach ($lists as $key => $value) {
            //$lists[$key]->type_name =  $this->get_type($value->type);
            $lists[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
            $lists[$key]['main_img'] = $config['cdnurl'] . $value['main_img'];
            $da = Db::name("notice_user")->where(['user_id'=>$user_id,'notice_id'=>$value['id']])->find();
            if($da){
                $lists[$key]['look'] = 1;
            }else{
                $lists[$key]['look'] = 0;
            }
        }
        $this->success('成功', $lists);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 详情
     */
    public function notice_deit(){
        $id = $this->request->post("id");
        if(!$id){
            $this->error('缺少参数');
        }
        $data = Db::name("notice")->where(['id'=>$id])->find();
        $up = Db::name("notice")->where('id','<',$id)->field('id,title')->order("id desc")->find();
        $low = Db::name("notice")->where('id','>',$id)->field('id,title')->order("id asc")->find();
        $re_data['info'] = $data;
        $re_data['up'] = $up;
        $re_data['low'] = $low;
         $this->success('成功', $re_data);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查看
     */
    public function notice_look(){
        $user_id = $this->auth->id;
        $id = $this->request->post("id");
        if(!$id){
            $this->error('缺少参数');
        }
        $da = Db::name("notice_user")->where(['user_id'=>$user_id,'notice_id'=>$id])->find();
        if($da){
            $this->success('成功');
        }else{
            $in_data['user_id'] = $user_id;
            $in_data['notice_id'] = $id;
            $in_data['createtime'] = time();
            $da = Db::name("notice_user")->insert($in_data);
            if($da){
                $this->success('成功');
            }else{
                $this->error('添加失败');
            }
        }
    }
}
